import type { App, Component } from 'vue'

// 使用 import.meta.glob 自动导入所有组件
const modules = import.meta.glob<{ default: Component }>('./nex-*/index.vue', {
	eager: true,
})

// 组件列表
const components: Component[] = []

// 组件导出对象
const componentExports: Record<string, Component> = {}

// 处理导入的组件
Object.entries(modules).forEach(([path, module]) => {
	const component = module.default
	if (component) {
		components.push(component)
		
		// 从路径中提取组件名: ./nex-button/index.vue -> NexButton
		const componentName = path
			.replace(/^\.\//, '')
			.replace(/\/index\.vue$/, '')
			.split('-')
			.map(word => word.charAt(0).toUpperCase() + word.slice(1))
			.join('')
		
		componentExports[componentName] = component
	}
})

// 定义 install 方法
const install = (app: App): void => {
	// 注册所有组件
	components.forEach(component => {
		const name = (component as any).name || (component as any).__name
		if (name) {
			app.component(name, component)
		}
	})
}

// 导出所有组件和 install 方法
export { install }
export default {
	install,
	...componentExports,
}
